import numpy as np
import csv
import random
from datetime import datetime
from time import strftime

'''
revision of test24
to calculate the false nagetive, false positive

'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = np.array([])
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            sKey =np.append(sKey, temp)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey

def cal_region_attri(region_id):
    dis_reg = np.unique(region_id)
    iLen = dis_reg.shape[0]
    temp_reg_attri = np.zeros((iLen, 3)) #[caner, pop, rate]

    # unit_attri = [cancer, pop, area, rate]

    i = 0
    for item in unit_attri:
        temp_reg_attri[int(region_id[i]),0] += item[0]
        temp_reg_attri[int(region_id[i]),1] += item[1]
        #temp_reg_attri[int(region_id[i]),2] + = item[2]
        i = i + 1

    for item in temp_reg_attri:
        item[2] = item[0]/item[1]

    return temp_reg_attri[:,3]


def cal_riskare_attri():
    temp_popcancer = np.array([])
    
    temp_cancer, temp_pop = cal_list_pop(H1)
    temp_popcancer = np.append(temp_popcancer, [temp_cancer, temp_pop])
    
    temp_cancer, temp_pop = cal_list_pop(L2)
    temp_popcancer = np.append(temp_popcancer, [temp_cancer, temp_pop])
    
    temp_cancer, temp_pop = cal_list_pop(H3)
    temp_popcancer = np.append(temp_popcancer, [temp_cancer, temp_pop])
    
    temp_cancer, temp_pop = cal_list_pop(H4)
    temp_popcancer = np.append(temp_popcancer, [temp_cancer, temp_pop])
    
    temp_cancer, temp_pop = cal_list_pop(L5)
    temp_popcancer = np.append(temp_popcancer, [temp_cancer, temp_pop])
    
    temp_cancer, temp_pop = cal_list_pop(L6)
    temp_popcancer = np.append(temp_popcancer, [temp_cancer, temp_pop])
    
    temp_cancer, temp_pop = cal_list_pop(H7)
    temp_popcancer = np.append(temp_popcancer, [temp_cancer, temp_pop])
    
    temp_popcancer.shape = (-1, 2)
    return temp_popcancer

def cal_list_pop(list):
    # calculate the total pop and cancer in the input list
    temp_pop = 0
    temp_cancer = 0
    # unit_attri = [cancer, pop, area, rate]
    for item in list:
        temp_pop = temp_pop + unit_attri[item, 1]
        temp_cancer = temp_cancer + unit_attri[item, 0]
    return temp_cancer, temp_pop


#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print "begin at " + getCurTime()

    H1 = [8,16,844,915,919,921,923,924]
    L2 = [5,103,106,513,517,518,520,531,534,535,536,541]
    H3 = [63,265,267,268,333,336,337,339,340,342,343,348]
    H4 = [13,174,178,198,886,887,888,889,890]
    L5 = [146,171,182,810,811,814,815,864,867]
    L6 = [20,133,692,694,695,696,698,702,705]
    H7 = [69,70,87,88,369,370,372,442,443]
    high_risk_area_id = H1 + H3 + H4 + H7
    low_risk_area_id = L2 + L5 + L6
    
    unitCSV = "C:/_DATA/CancerData/test/Mar21/pvalue/16_4polygon/TP1000_16_4polygon.csv"
    unit_attri = np.zeros((1000,2))
    dataMatrix = build_data_list(unitCSV)  # [ID, pop, cancer1, cancer2, cancer3]
    unit_attri[:,1] = dataMatrix[:,1]

    repeat = 1
    riskarea_attri = np.array([])

    #while repeat < 1001:
    while repeat < 101:
        unit_attri[:,0] = dataMatrix[:,repeat+1] # [cancer, pop, area, rate, id]
        temp_riskarea_attri = cal_riskare_attri()  # [total_cancer, total_pop, total_area]
        riskarea_attri = np.append(riskarea_attri, temp_riskarea_attri[:,0])
        riskarea_attri = np.append(riskarea_attri, temp_riskarea_attri[:,0]*100/temp_riskarea_attri[:,1])
        repeat = repeat + 1
    riskarea_attri = np.append(riskarea_attri, temp_riskarea_attri[:,1])
    riskarea_attri.shape = (-1,7)
    print riskarea_attri
    
    filePath = unitCSV[:-4] + "_riskarea_attri.csv"
    np.savetxt(filePath, riskarea_attri, delimiter=',')

    print "end at " + getCurTime()
    print "========================================================================"  

           
